package id.xsis.project.blog.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

	@RequestMapping(value="/master/*", method = RequestMethod.GET)
	public String userControl(HttpServletRequest request){
		
		String link="";
		if(request.isUserInRole("ROLE_ADMIN")){
			link = "/admin/index.htm";
		}
		if(request.isUserInRole("ROLE_USER")){
			link = "/user/index.htm";
		}
		
		return "redirect:"+link;		
	}
	
	@RequestMapping(value="login.htm", method = RequestMethod.GET)
	public String login(ModelMap model) {
 
		return "login";
	}
	
	@RequestMapping(value="loginfailed.htm", method = RequestMethod.GET)
	public String loginerror(ModelMap model) {
 
		model.addAttribute("error", "true");
		return "login";
	}
	
	@RequestMapping(value="index.htm", method = RequestMethod.GET)
	public String logout(ModelMap model) {
 
		return "login";
	}
}
